package com.mall.web.controller.product;

import com.mall.common.annotation.Log;
import com.mall.common.core.controller.BaseController;
import com.mall.common.core.domain.AjaxResult;
import com.mall.common.core.page.TableDataInfo;
import com.mall.common.enums.BusinessType;
import com.mall.common.utils.poi.ExcelUtil;
import com.mall.product.domain.biz.ProSkuBiz;
import com.mall.product.domain.entity.ProSku;
import com.mall.product.service.ProSkuService;
import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

/**
 * SKUController
 *
 * @author zhonglin
 * @date 2020-05-07
 */
@Controller
@RequestMapping("/product/sku")
public class ProSkuController
    extends BaseController {

  private String prefix = "product/sku";

  @Autowired
  private ProSkuService proSkuService;

  @RequiresPermissions("product:sku:view")
  @GetMapping()
  public String sku() {

    return prefix + "/sku";
  }

  /**
   * 查询SKU列表
   */
  @RequiresPermissions("product:sku:list")
  @PostMapping("/list")
  @ResponseBody
  public TableDataInfo list(ProSku proSku) {

    startPage();
    List<ProSkuBiz> list = proSkuService.selectProSkuList(proSku);
    return getDataTable(list);
  }

  /**
   * 导出SKU列表
   */
  @RequiresPermissions("product:sku:export")
  @Log(title = "SKU", businessType = BusinessType.EXPORT)
  @PostMapping("/export")
  @ResponseBody
  public AjaxResult export(ProSku proSku) {

    List<ProSkuBiz>      list = proSkuService.selectProSkuList(proSku);
    ExcelUtil<ProSkuBiz> util = new ExcelUtil<ProSkuBiz>(ProSkuBiz.class);
    return util.exportExcel(list, "sku");
  }

  /**
   * 新增SKU
   */
  @GetMapping("/add")
  public String add() {

    return prefix + "/add";
  }

  /**
   * 新增保存SKU
   */
  @RequiresPermissions("product:sku:add")
  @Log(title = "SKU", businessType = BusinessType.INSERT)
  @PostMapping("/add")
  @ResponseBody
  public AjaxResult addSave(ProSku proSku) {

    return toAjax(proSkuService.insertProSku(proSku));
  }

  /**
   * 修改SKU
   */
  @GetMapping("/edit/{skuId}")
  public String edit(@PathVariable("skuId") Long skuId,
                     ModelMap mmap) {

    ProSkuBiz proSku = proSkuService.selectProSkuById(skuId);
    mmap.put("proSku", proSku);
    return prefix + "/edit";
  }

  /**
   * 修改保存SKU
   */
  @RequiresPermissions("product:sku:edit")
  @Log(title = "SKU", businessType = BusinessType.UPDATE)
  @PostMapping("/edit")
  @ResponseBody
  public AjaxResult editSave(ProSku proSku) {

    return toAjax(proSkuService.updateProSku(proSku));
  }

  /**
   * 删除SKU
   */
  @RequiresPermissions("product:sku:remove")
  @Log(title = "SKU", businessType = BusinessType.DELETE)
  @PostMapping("/remove")
  @ResponseBody
  public AjaxResult remove(String ids) {

    return toAjax(proSkuService.deleteProSkuByIds(ids));
  }

  /**
   * 批量新增保存SKU
   */
  @RequiresPermissions("product:sku:add")
  @Log(title = "SKU", businessType = BusinessType.INSERT)
  @PostMapping("/batch-add")
  @ResponseBody
  public AjaxResult batchAddSave(@RequestBody List<ProSku> proSkus) {

    return toAjax(proSkuService.batchInsertProSku(proSkus));
  }

}
